package com.ym.pms.controller;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.ym.pms.model.Contract;
import com.ym.pms.model.SysUser;
import com.ym.pms.service.ContractServices;
import com.ym.pms.service.SysUserService;
import com.ym.pms.service.impl.PdfService;
import com.ym.pms.utils.JSONResult;
import freemarker.template.Configuration;
import freemarker.template.Template;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author: kevin
 * @Date: 2018/11/16
 */
@RestController
@Api(tags = "生成合同pdf")
public class PdfController {

    @Autowired
    private PdfService pdfService;

    @ApiOperation(value = "生成合同")
    @RequestMapping(value = "createContractPdf")
    public void showPdf(HttpServletResponse response,
                        @RequestParam Integer id) {
        pdfService.createContract(id);
        pdfService.readPdf(response, "/www/wwwroot/pms/contract/"+id+".pdf");
    }

    @ApiOperation(value = "下载合同")
    @RequestMapping(value = "getContractPdf")
    public JSONResult getPdf(HttpServletResponse response,
                             @RequestParam Integer id) {
        String file = pdfService.getPdf(id);
        return JSONResult.ok(file);
    }


}
